<?php

!defined('IN_ROOT') && exit('Access Denied');

class admin_sitecontrol extends base {

    function admin_sitecontrol(& $get,& $post) {
        $this->base( & $get,& $post);
        $this->load('site');
        $this->load('area');
        $this->load('sysmap');
        $this->load('cate');
    }

    function ondefault($message='') {
        if(empty($message)) unset($message);
        $sitelist = $_ENV['site']->get_list();
        include template('sitelist','admin');
    }

    function onadd() {
        if(isset($this->post['submit'])) {
        	$aid = (int)$this->post['pid'];
        	$ifopen = (int)$this->post['ifopen'];
        	$name = trim($this->post['name']);
        	$pinyin = getpinyin($name);
        	$dir = isset($this->post['dir'])?$this->post['dir']:$pinyin;
        	$domain = isset($this->post['domain'])?$this->post['domain']:$pinyin;
        	$py = strtoupper(substr($pinyin,0,1));
        	$keywords = $this->post['keywords'];
            $descs = $this->post['descs'];
            $tpls = $this->post['tpls'];
            $available = (int)$this->post['available'];
            $outcate = $this->post['outcate'];
            //var_dump($outcate);exit();
            $displayorder = 0;
            if(!$name) {
                $type='errormsg';
                $message = '分站名称不能为空!';
                include template('addsite','admin');
                exit;
            }
            $sid = $_ENV['site']->add($aid,$dir,$ifopen,$name,$py,$domain,$keywords,$descs,$tpls,$displayorder,$available);
            $_ENV['sysmap']->insiteout($sid,$outcate);
            $this->cache->remove('site');
            $this->ondefault('分站添加成功！');
        }else {
        	$area_js=$_ENV['area']->select_option_cate();
        	$out_js = $_ENV['sysmap']->get_site();
            include template('addsite','admin');
        }
    }

    function onedit() {
        if(isset($this->post['submit'])) {
            $aid = (int)$this->post['pid'];
        	$ifopen = (int)$this->post['ifopen'];
        	$name = trim($this->post['name']);
        	$pinyin = getpinyin($name);
        	$dir = isset($this->post['dir'])?$this->post['dir']:$pinyin;
        	$domain = isset($this->post['domain'])?$this->post['domain']:$pinyin;
        	$py = strtoupper(substr($pinyin,0,1));
        	$keywords = $this->post['keywords'];
            $descs = $this->post['descs'];
            $tpls = $this->post['tpls'];
            $available = (int)$this->post['available'];
            $nid = intval($this->post['nid']);
            $outcate = $this->post['outcate'];
            if(!$name) {
                $type='errormsg';
                $message = '分站名称不能为空!';
                $cursite = $_ENV['site']->get($nid);
                include template('addsite','admin');
                exit;
            }
            $_ENV['site']->update($aid,$dir,$ifopen,$name,$py,$domain,$keywords,$descs,$tpls,$available,intval($nid));
            $_ENV['sysmap']->delsiteout($nid);
            $_ENV['sysmap']->insiteout($nid,$outcate);
            $this->cache->remove('site');
            $this->ondefault('分站修改成功！');
        }else {
            $cursite = $_ENV['site']->get(intval($this->get[2]));
            $area = $this->area[$cursite[aid]][id];
            $area_js=$_ENV['area']->select_option_cate($area);
            $out_js = $_ENV['sysmap']->get_site($cursite['id']);
            include template('addsite','admin');
        }
    }
    function oncates() {
    	if(isset($this->post['submit'])) {
    		$sid = (int)$this->post['sid'];
    		$typeid = (int)$this->post['tid'];
    		$cates = $this->post['cates'];
            $_ENV['sysmap']->delsitecate($sid,$typeid);
            $_ENV['sysmap']->insitecate($sid,$typeid,$cates);
            msg("编辑成功!",'index.php?admin_site/cates/'.$sid.'/'.$typeid);
    		
    	}else {
    		$sid = intval($this->get[2]);
        	$site = $_ENV['site']->get($sid);
        	$typeid = intval($this->get[3]);
        	if($typeid == 1){$nv = '周边游';}
        	elseif($typeid == 2){$nv = '国内游';}
        	else{$nv = '出境游';}
        	$list = $_ENV['sysmap']->get_sitelist($sid,$typeid);
        	include template('sitecates','admin');
    	}
    }
    function onareas() {
        if(isset($this->post['submit'])) {
        	$aid = (int)$this->post['aid'];
        	$sid = (int)$this->post['sid'];
        	$cid = (int)$this->post['cid'];
        	$type = (int)$this->post['type'];
        	if(is_array($this->post['ename'])){
        		foreach ($this->post['ename'] as $ek => $ev){
        			if(!empty($ev)){
        				$_ENV['sysmap']->addareatag($ev,$ek);
        			}
        		}
        	}
        	
        	if(!empty($this->post['delaid'])){
        		$cids = implode(",",$this->post['delaid']);
        		$_ENV['sysmap']->delcate($sid,$type,$cid,$cids);
        	}
			if(!empty($this->post['addaid'])){
            	$_ENV['sysmap']->addcate($sid,$type,$cid,$this->post['addaid']);
			}
			
            msg('设置成功！',"index.php?admin_site/areas/{$sid}/{$type}/{$cid}/{$aid}");
        }else {
        	$sid = intval($this->get[2]);
        	$site = $_ENV['site']->get($sid);
        	$type = intval($this->get[3]);
        	if($type == 1){$nv = '国内游';}
        	elseif($type == 2){$nv = '周边游';}
        	else{$nv = '出境游';}
        	//$category['grade'] = $pid =0;
        	$cid = intval($this->get[4]);
        	$aid = intval($this->get[5]);
        	$category = $this->area[$aid];
        	//list_by_cid_pid_area($aid,$pid,$cid,$sid,$type=0)
        	$cname = $_ENV['cate']->get_catename($cid);
        	$categorylist = $_ENV['sysmap']->list_by_cid_pid_area($aid,$category['pid'],$cid,$sid,$type);
        	$navlist=$_ENV['sysmap']->get_navigation_area($aid);
            include template('siteareas','admin');
        }
    }
	function onsort() {
        if(isset($this->post['submit'])) {
        	$pid = (int)$this->post['pid'];
        	$sid = (int)$this->post['sid'];
        	if(!empty($this->post['cids'])){
        		$cids = implode(",",$this->post['cids']);
        		$_ENV['map']->delcateout($sid,$cids);
        	}
			if(!empty($this->post['cid'])){
            	$_ENV['map']->addcateout($sid,$this->post['cid']);
			}
            msg('设置成功！',"index.php?admin_site/sort/{$sid}/$pid");
        }else {
        	$sid = intval($this->get[2]);
        	$site = $_ENV['site']->get($sid);
        	//$category['grade'] = $pid =0;
        	$pid = intval($this->get[3]);
        	$category = $this->category[$pid];
        	if($pid && $_ENV['map']->check_has_map_out($sid,$pid)==0){
        		msg('请选择该类后再进行下级操作！',"index.php?admin_site/sort/{$sid}/$category[pid]");
        	}	
        	$categorylist = $_ENV['map']->list_by_cid_pid_site_out($pid,$category['pid'],$sid);
        	$navlist=$_ENV['map']->get_navigation($pid);
            include template('sitesort','admin');
        }
    }
    function oncate() {
        if(isset($this->post['submit'])) {
        	$pid = (int)$this->post['pid'];
        	$sid = (int)$this->post['sid'];
        	if(!empty($this->post['cids'])){
        		$cids = implode(",",$this->post['cids']);
        		$_ENV['map']->delcate($sid,$cids,1);
        	}
			if(!empty($this->post['cid'])){
            	$_ENV['map']->addcate($sid,$this->post['cid'],1);
			}
            msg('设置成功！',"index.php?admin_site/cate/{$sid}/$pid");
        }else {
        	$sid = intval($this->get[2]);
        	$site = $_ENV['site']->get($sid);
        	//$category['grade'] = $pid =0;
        	$pid = intval($this->get[3]);
        	$category = $this->category[$pid];
        	if($pid && $_ENV['map']->check_has_map($sid,$pid,1)==0){
        		msg('请选择该类后再进行下级操作！',"index.php?admin_site/cate/{$sid}/$category[pid]");
        	}	
        	$categorylist = $_ENV['map']->list_by_cid_pid_site($pid,$category['pid'],$sid,1);
        	$navlist=$_ENV['map']->get_navigation($pid);
            include template('sitecate','admin');
        }
    }
    function onarea() {
        if(isset($this->post['submit'])) {
        	$pid = (int)$this->post['pid'];
        	$sid = (int)$this->post['sid'];
        	$type = (int)$this->post['type'];
        	$tagids = array();
        	if(is_array($this->post['ename'])){
        		foreach ($this->post['ename'] as $ek => $ev){
        			if(!empty($ev)){
        				$tagids[$ek] = $_ENV['tag']->addareatag($ev,$ek);
        			}
        		}
        	}
        	
        	if(!empty($this->post['cids'])){
        		$cids = implode(",",$this->post['cids']);
        		$_ENV['map']->delcate($sid,$cids,$type);
        	}
			if(!empty($this->post['cid'])){
            	$_ENV['map']->addcate($sid,$this->post['cid'],$type,$tagids);
			}
			
            msg('设置成功！',"index.php?admin_site/area/{$sid}/{$type}/{$pid}");
        }else {
        	$sid = intval($this->get[2]);
        	$site = $_ENV['site']->get($sid);
        	$type = intval($this->get[3]);
        	if($type == 3){$nv = '国内游';}
        	elseif($type == 4){$nv = '周边游';}
        	else{$nv = '出境游';}
        	//$category['grade'] = $pid =0;
        	$pid = intval($this->get[4]);
        	$category = $this->area[$pid];
        	if($pid && $_ENV['map']->check_has_map($sid,$pid,$type)==0){
        		msg('请选择该类后再进行下级操作！',"index.php?admin_site/area/{$sid}/{$type}/$category[pid]");
        	}	
        	$categorylist = $_ENV['map']->list_by_cid_pid_area($pid,$category['pid'],$sid,$type);
        	$navlist=$_ENV['map']->get_navigation_area($pid);
            include template('sitearea','admin');
        }
    }
    function onremove() {
        $_ENV['site']->remove_by_id(intval($this->get[2]));
        $this->cache->remove('site');
        $this->ondefault('分站刪除成功！');
    }

    function onreorder() {
        $orders = explode(",",$this->post['order']);
        $hid = intval($this->post['hiddencid']);
        foreach($orders as $order => $lid) {
            $_ENV['site']->order_site(intval($lid),$order);
        }
    }
    function ositeailable() {
        $available = intval($this->get[3])?0:1;
        $_ENV['site']->update_available(intval($this->get[2]),$available);
        $this->cache->remove('site');
        $message=$available?'分站启用成功!':'分站禁用成功!';
        $this->ondefault($message);
    }

}
?>